package com.ljl.ningwei.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljl.ningwei.common.exception.ClientException;
import com.ljl.ningwei.common.utils.JWTUtil;
import com.ljl.ningwei.dto.UserInfoDTO;
import com.ljl.ningwei.entity.User;
import com.ljl.ningwei.mapper.UserMapper;
import com.ljl.ningwei.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 文件服务
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;
    private final ObjectMapper jsonMapper;

    @Override
    public User getById(Long id) {
        return userMapper.getById(id);
    }

    @Override
    public User extractById(Long id) {
        return userMapper.extractById(id);
    }

    @Override
    public Long edit(User requestParam) {
        if (ObjectUtils.isEmpty(requestParam.getId())) {
            try {
                userMapper.insert(requestParam);
            } catch (DuplicateKeyException ex) {
                throw new RuntimeException("用户账号:" + requestParam.getAccount() + "已经存在");
            }
        } else {
            if (getById(requestParam.getId()) == null) {
                throw new RuntimeException("用户" + requestParam.getId() + " 不存在，编辑失败");
            }
            userMapper.update(requestParam);
        }
        return requestParam.getId();
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public List<User> list() {
        return null;
    }

    @Override
    public void register(User user) {
        String password = user.getPassword();
        user.setPassword(passwordEncoder.encode(password));
        edit(user);
    }

    @Override
    @SneakyThrows
    public String login(String account, String password) {
        User user = User
                .builder()
                .account(account)
                .build();
        User queryUser = userMapper.query(user);
        if (ObjectUtils.isEmpty(queryUser)) {
            throw new ClientException("用户账号不存在");
        }
        boolean matches = passwordEncoder.matches(password,queryUser.getPassword());
        // 不匹配
        if (!matches) {
            throw new ClientException("用户密码不正确");
        }
        UserInfoDTO userInfoDTO = new UserInfoDTO()
                .setId(queryUser.getId())
                .setName(queryUser.getName());
        String token = JWTUtil.createJWT(jsonMapper.writeValueAsString(userInfoDTO));
        // 生成token
        return token;
    }

}
